//
//  XTWelcomViewController.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/13.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class XTWelcomViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //添加子控件
        setUpUI()
        ///加载用户头像
        loadUserIconView()
        
        ///请求数据
        XTUserAccountViewModel.sharedInstance.loadUserMessage { (error) -> () in
            if error != nil {
                XTPrint(items: "welcome获取用户数据回调出现错误: \(error)")
            }else {
                ///加载用户头像
                self.loadUserIconView()
            }
        }
        
    }
    
    
    ///加载用户头像
    private func loadUserIconView() {
        
        if let urlStr = XTUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: urlStr)
            self.userIconView.sd_setImageWithURL(url, placeholderImage:UIImage(named: "avatar_default_big"))
            
        }
    
    }
    

    
    override func viewDidAppear(animated: Bool) {
        
        //弹起的动画
        startAnimation()
    }
    
    //MARK: - 设置UI
   private func setUpUI() {
        
        //放到懒加载中
//       userIconView.layer.cornerRadius = 42.5
//        userIconView.layer.masksToBounds = true
        
        //添加UI
        self.view.addSubview(bgimageView)
        self.view.addSubview(userIconView)
        self.view.addSubview(welcomLabel)
        
        //设置约束
        setConstraint()
    }
    
    
    //设置约束
    private func setConstraint() {
        
        //背景图
        bgimageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        
        //用户头像
        userIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
        }
        
        //欢迎文字
        welcomLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(userIconView)
            make.top.equalTo(userIconView.snp_bottom).offset(16)
        }
        
    }
    
    
    //MARK: - 动画
   private func startAnimation() {
        /**
        弹簧动画
        
        - parameter <Tduration:             动画执行时间
        - parameter delay:                  动画延迟时间
        - parameter usingSpringWithDamping: 弹簧的明显程度 0 - 1
        - parameter initialSpringVelocity:  初始速度
        - parameter options:                设置选项
        - parameter animations:             动画执行块
        - parameter completion:             动画完成后执行块
        */
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 30, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            //改变背景图的frame
            self.userIconView.snp_updateConstraints(closure: { (make) -> Void in
                
            make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            
            //根据约束重新设置frame值
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                XTPrint(items: "动画完成")
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.welcomLabel.alpha = 1
                    }, completion: { (_) -> Void in
                      let Delegate = UIApplication.sharedApplication().delegate as! AppDelegate
                        Delegate.changeRootViewController(XTMainTabBarController())
                })
        }
    
    }
    
    

    //MARK: - 懒加载
    /// 背景图
   private lazy var bgimageView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    ///用户头像
    private lazy var userIconView : UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //设置圆角
        imageView.layer.cornerRadius = 42.5
        //蒙板
        imageView.layer.masksToBounds = true

        return imageView
    }()
    
    //欢迎文字
   private lazy var welcomLabel : UILabel = {
        var label = UILabel()
        
        //设置
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.blackColor()
        //自适应
        label.sizeToFit()
        label.alpha = 0
        
        return label
    }()

}
